<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>The Ajax 02 Page</h1>
<button onclick="doAjaxGet()">Do Ajax Get</button>
<button onclick="doAjaxPost()">Do Ajax Post</button>
<button onclick="doAjaxDelete()">Do Ajax Delete</button>
<button onclick="doAjaxUpdate()">Do Ajax Update</button>
<!--<div id="result"></div>-->
<form id="result">
    <table>
        <thead>
        <tr>
        <th>id</th>
        <th>name</th>
        <th>parentId</th>
        <th>remark</th>
        <th>createdTime</th>
        </tr>,
        </thead>
        <tbody>
        <tr each="List">
            <td text="id">null</td>
            <td text="name">null</td>
            <td text="parentId">null</td>
            <td text="remark">null</td>
            <td text="createdTime">null</td>
        </tr>
        </tbody>
    </table>
</form>
<script>
    function doAjaxGet() {
        //1.创建XHR对象
        //2.设置状态监听
        //3.建立连接
        //4.发送请求
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4){
                if (xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
        }
        xhr.open("Get","http://localhost/doAjaxGet",true);
        xhr.send(null);
    }

    function doAjaxPost() {
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4){
                if (xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
        }
        xhr.open("POST","http://localhost/doAjaxPost",true);
        //post请求向服务端传递数据，需要设置请求头，必须在open之后
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //发送请求(post请求传递数据，需要将数据写入到send方法内部)
        xhr.send("id=101&name=Computer&remark=Computer...");
    }

    function doAjaxUpdate() {
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4){
                if (xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
        }
        xhr.open("put","http://localhost/doAjaxUpdate",true);
        //post请求向服务端传递数据，需要设置请求头，必须在open之后
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //发送请求(post请求传递数据，需要将数据写入到send方法内部)
        xhr.send("id=101&name=Book&remark=Book...");
    }

    function doAjaxDelete() {
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4){
                if (xhr.status==200){
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
        }
        xhr.open("delete","http://localhost/doAjaxDelete?id=101",true);
        xhr.send(null);
    }
    (function (){
        var ajax=function () {}
        ajax.prototype={
            ajaxGet:function(url,params,callback){

            }
        }
    })
</script>
</body>
</html>